#!/usr/bin/env python
import time
import pygame

# global constants
FREQ = 44100   # same as audio CD
BITSIZE = -16  # unsigned 16 bit
CHANNELS = 2   # 1 == mono, 2 == stereo
BUFFER = 1024  # audio buffer size in no. of samples
FRAMERATE = 30 # how often to check if playback has finished

def playSound(soundFileName):
    """Play sound through default mixer channel in blocking manner.

    This will load the whole sound into memory before playback
    """

    sound = pygame.mixer.Sound(soundFileName)
    clock = pygame.time.Clock()
    sound.play()
    while pygame.mixer.get_busy():
        clock.tick(FRAMERATE)


pygame.mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)

INIT_TIME = 40
TIME_1 = 20
TIME_2 = 10
TOTAL_TIME = 1*60*60 + 15*60 # on met un peu plus au cas ou, au pire on arretera le programme a la main
soundFileName1 = "power_up.wav"
soundFileName2 = "power_of.wav"


currentTime = 0
time.sleep(INIT_TIME)

while currentTime <= TOTAL_TIME:
    lastTime = time.time()
    playSound(soundFileName1)
    time.sleep(TIME_1 + lastTime - time.time())
    currentTime+=TIME_1
    lastTime = time.time()
    playSound(soundFileName2)
    time.sleep(TIME_2 + lastTime - time.time())
    currentTime+=TIME_2




